<!DOCTYPE html>
<html class="um landscape min-width-240px min-width-320px min-width-480px min-width-768px min-width-1024px">
    <head>
        <title></title>
        <meta charset="utf-8">
        <meta name="viewport" content="target-densitydpi=device-dpi, width=device-width, initial-scale=1, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <link rel="stylesheet" href="css/ui-base.css">
        <link rel="stylesheet" href="css/ui-box.css">
        <link rel="stylesheet" href="css/ui-color.css">
        <link rel="stylesheet" href="index_content/css/main.css">
        <link rel="stylesheet" href="css/fonts/font-awesome.min.css">
        <link rel="stylesheet" href="css/appcan.icon.css">
        <link rel="stylesheet" href="css/appcan.control.css">
        <link rel="stylesheet" href="index/css/main.css">
    </head>
    <body class="um-vp bc-bg" ontouchstart>
        <div class="umar-a ub ub-ac ub-f1">
            <div class="sc-bg-active uinput ub ub-f1  bc-border uba">
                
                <input placeholder="  请输入搜索书籍" type="text" class="ub-f1" id="search">
            
               <div class="uinn fa fa-search sc-text" onclick="dododo()"></div>
            </div>
        </div>
        <div class="ub ub-ver uinn-b1" id="bookList">

        </div>
    </body>
    <script src="js/appcan.js"></script>
    <script src="js/appcan.control.js"></script>
    </body>
    <script>
        var username = localStorage.getItem("username");
        appcan.ready(function() {
            var request = appcan.require('request');
            var arr = [];
            var HasSearch = localStorage.getItem('HasSearch');
if (HasSearch == null) HasSearch = 0;
if (username == null) username = "1";
            if (HasSearch == 0) {
                request.ajax({
                    type : 'POST',
                    url : 'http://whynothappy.sinaapp.com/homepage',
                    data : {username : username , start_book_no : "1", end_book_no : "100"},
                    contentType: 'application/json',
                    success:function(data){
                        //alert(data);
                        bookList(eval("(" + data + ")"));
                    },
                    error:function(){
                        alert("can't connect the Internet");
                    }
                })
            }
            else
            {
                var book_name = localStorage.getItem('book_name');
                request.ajax({
                    type : 'POST',
                    url : 'http://whynothappy.sinaapp.com/search_book',
                    data : {username : username , book_name : book_name},
                    contentType: 'application/json',
                    success:function(data){
                        //alert(data);
                        bookList(eval("(" + data + ")"));
                    },
                    error:function(){
                        alert("can't connect the Internet");
                    }
                })
            }
        });
        var arrBook = [];
        function Add(data) {
            arrBook.push({"isbn":data.isbn, "bookImg":data.pic_url, "bookName":data.title});
            //alert(arrBook);
            var tmpl = '${cb:begin}' + 
            '<div class="ub ub-ver uw-rli" id="${isbn}" onmousedown="appcan.touch(\'\')" ontouchstart="appcan.touch(\'\')" onclick="read(event);">' + 
            '   <div  style="background-image:url(${bookImg});" class="ub-img umh-list8 umw-list6  umar-ar6">' + 
            '  </div>' + 
            ' <div class="t-gra-4b tx-c ulev-4 uof uinn-r1 umar-ar6">${bookName}</div>' + 
            '</div>${cb:end}'
            var s = appcan.tempRenderList(tmpl, arrBook, appcan.getObjLength(arrBook), tmpleCb);

            
            $('#bookList')[0].innerHTML = s;
        }
        function bookList(data) {
            var request = appcan.require('request');
            for(var i = 0; i < 30; ++i)
            {
                request.ajax({
                    type : 'POST',
                    url : 'http://whynothappy.sinaapp.com/bookinfo',
                    data : {username : username , isbn : data.books[i].isbn},
                    contentType: 'application/json',
                    success:function(data){
                        Add(eval("(" + data + ")"));
                    },
                    error:function(){
                        alert("can't connect the Internet");
                    }
                })
            }
        }

        var i = 0;
        function tmpleCb(a, b) {
            switch (b[1]) {
            case 'begin':
                ++i;
                if (i % 3 == 1) {
                    return '<div class="ub ub-at-list ub-f1 umar-at1">'
                }
                return '';
            case 'end':
                if (i % 3 == 0) {
                    return '</div>'
                }
                return '';
            }
        }

        function read(e) {
            if (appcan.detect.os.phone && e.type == 'click')
                return;
            //alert(e.currentTarget.id);
            localStorage.setItem('isbn',e.currentTarget.id);
            appcan.openWinWithUrl('catalog', 'book_info.html');
        }
        
        function dododo(){
            localStorage.setItem('HasSearch', 1);
            localStorage.setItem('book_name', $("#search")[0].value);
            var HasSearch = localStorage.getItem('HasSearch');
            window.location.reload();
        }
    </script>
</html>
